﻿using HandyControl.Data;
using Super.Hmi.Commands;
using System.Windows.Controls;
using static Super.Hmi.Assets.CustomControl.PageControl;

namespace Super.Hmi.ViewModels.Base
{
    /// <summary>
    /// 所有弹窗的基类
    /// </summary>
    public abstract class PageViewModelBase : ViewModelBase
    {
        private bool _showModifyDrawer;
        /// <summary>
        /// 是否显示修改抽屉
        /// </summary>
        public bool ShowModifyDrawer
        {
            get { return _showModifyDrawer; }
            set { SetProperty<bool>(ref _showModifyDrawer, value); }
        }
        /// <summary>
        /// 刷新
        /// </summary>
        public virtual void DoRefresh() { DoSearch(); }
        #region 方法
        public RelayCommand ResetCommand { get; set; } // 重置
        public RelayCommand SearchCommand { get; set; } // 搜索
        public RelayCommand BatchDeleteCommand { get; set; } // 批量删除
        public RelayCommand ModifyCommand { get; set; } // 修改
        public RelayCommand<SelectionChangedEventArgs> DataGridSelectionCommand { get; set; } // 全选
        public RelayCommand ShowModifyDrawerCommand { get; set; } // 显示修改抽屉
        #endregion
        #region 分页
        private int _pageCount = 10;
        /// <summary>
        /// 总页数
        /// </summary>
        public int PageCount
        {
            get { return _pageCount; }
            set { SetProperty<int>(ref _pageCount, value); }
        }
        private PageSizeType _pageSize = PageSizeType.二十;
        /// <summary>
        /// 每页显示数量
        /// </summary>
        public PageSizeType PageSize
        {
            get { return _pageSize; }
            set
            {
                SetProperty<PageSizeType>(ref _pageSize, value);
                DoRefresh();
            }
        }
        private int _pageIndex = 1;
        /// <summary>
        /// 当前页码
        /// </summary>
        public int PageIndex
        {
            get { return _pageIndex; }
            set { SetProperty<int>(ref _pageIndex, value); }
        }
        private int _totalCount;
        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalCount
        {
            get { return _totalCount; }
            set { SetProperty<int>(ref _totalCount, value); }
        }
        public RelayCommand<FunctionEventArgs<int>> PageUpdatedCommand { get; set; } // 页码更新
        #endregion
        public PageViewModelBase()
        {
            ResetCommand = new RelayCommand(DoReset);
            SearchCommand = new RelayCommand(DoSearch);
            BatchDeleteCommand = new RelayCommand(DoBatchDelete);
            ModifyCommand = new RelayCommand(DoModify);
            PageUpdatedCommand = new RelayCommand<FunctionEventArgs<int>>(DoPageUpdated);
            DataGridSelectionCommand = new RelayCommand<SelectionChangedEventArgs>(DoDataGridSelection);
            DoReset();
        }
        /// <summary>
        /// 处理数据表格选择事件
        /// </summary>
        /// <param name="args"></param>
        public virtual void DoDataGridSelection(SelectionChangedEventArgs args) { }
        /// <summary>
        /// 处理页码更新事件
        /// </summary>
        /// <param name="args"></param>
        public virtual void DoPageUpdated(FunctionEventArgs<int> args)
        {
            PageIndex = args.Info;
            DoRefresh();
        }
        /// <summary>
        /// 处理显示修改事件
        /// </summary>
        public virtual void DoModify() { }
        /// <summary>
        /// 执行搜索操作
        /// </summary>
        public virtual void DoSearch() { }
        /// <summary>
        /// 重置视图模型
        /// </summary>
        public virtual void DoReset() { }
        /// <summary>
        /// 执行批量删除操作
        /// </summary>
        public virtual void DoBatchDelete() { }
    }
    /// <summary>
    /// 有数据模型的页面基类
    /// </summary>
    /// <typeparam name="TModel"></typeparam>
    public class PageViewModelBase<TModel> : PageViewModelBase
    {
        public RelayCommand<TModel> DeleteCommand { get; set; }  // 删除
        public PageViewModelBase()
        {
            DeleteCommand = new RelayCommand<TModel>(DoDelete);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="model"></param>
        public virtual void DoDelete(TModel model) { }
    }
}
